/**
 * Created by 121 on 2016/4/06.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'highCharts'
], function ($, common, ready, template, Collection, Model, dialog, calendar, highCharts) {
        var index = function () {
            this.init();

        };
        index.prototype = {
            init: function () {
                this.event();
                this.fetch();
            },
            event: function () {
                var _this = this;
                $.ajax({
                    url: _global.url.api + 'site/data/pv_uv_data',
                    type: 'get',
                    success: function (response) {
                        if (response.ret == 0) {
                            $('#pvuv-data-views').append(template('pvuv-data-tpl', response.data));
                        }
                    }
                });

                $.ajax({
                    url: _global.url.api + 'site/data/pv_uv_curve',
                    type: 'get',
                    success: function (response) {
                        if (response.ret == 0) {
                            $('#pvuv-curve-views').html(template('pvuv-curve-tpl'));
                            $(function () {
                                $('#pvuv_data').highcharts({
                                    title: {
                                        text: '昨日今日PV、UV',
                                        x: -20 //center
                                    },
                                    xAxis: {
                                        categories: response.data.categories
                                    },
                                    yAxis: {
                                        title: {
                                            text: ''
                                        },
                                        min: 0,
                                        plotLines: [{
                                            value: 0,
                                            width: 0,
                                            color: '#808080'
                                        }]
                                    },
                                    tooltip: {
                                        valueSuffix: ''
                                    },
                                    legend: {
                                        layout: 'vertical',
                                        align: 'right',
                                        verticalAlign: 'middle',
                                        borderWidth: 0
                                    },
                                    series: [{
                                        name: '昨日UV',
                                        data: response.data.yesterday.c_uv
                                    }, {
                                        name: '今日UV',
                                        data: response.data.today.c_uv
                                    }, {
                                        name: '昨日PV',
                                        data: response.data.yesterday.c_pv
                                    }, {
                                        name: '今日PV',
                                        data: response.data.today.c_pv
                                    }]
                                });
                            });
                        }
                    }
                });
                //实时产品销售排行
                $.ajax({
                    url: _global.url.api + 'site/data/real_time_hot_goods',
                    type: 'get',
                    success: function (res) {
                        if (res && res.ret == 0) {
                            for (var k = 0; k < res.data.length; k++) {
                                var item = res.data[k];
                                //console.log('item', item);
                                $('#data-list-item tbody').append(template('real-goods-sale-tpl', item));
                            }
                        }
                    }
                });
            },
            //实时来源
            fetch: function () {
                var _this = this;
                $('#real-goods-sale-list-view').append(template('real-url-tpl'));
                this.collection = new Collection({
                    container: 'data-list tbody',
                    template: 'real-url-item-tpl',
                    url: _global.url.api + 'site/data/real_url',
                    options : {
                        'limit': 10,
                        'offset': 0,
                        'sort': 'dt_add',
                        'order': 'desc'
                    },
                    model: {
                        tag_name: 'tr',
                        url: _global.url.api + '',
                        event: {}
                    },
                    callback: function () {
                    }
                });
                this.collection.fetch();
            }
        };
        new index();
    });

